package cn.tedu.csmall.stock.webapi.timer;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {
    //把Job的实现类封装到JobDetail对象中
    //并交给Spring容器管理
    @Bean
    public JobDetail showTime(){
        System.out.println("showTime方法运行了");
        return JobBuilder.newJob(QuartzJob.class)
                .withIdentity("dateTime") //给JobDetail起名字
                .storeDurably() //即使没有触发器绑定当前JobDetail，也存在
                .build();
    }
    //声明触发器，它设置Job何时运行，也需要保存到Spring容器中
    @Bean
    public Trigger showTimeTrigger(){
        System.out.println("showTimeTrigger方法运行");
        //定义cron表达式
        CronScheduleBuilder cronScheduleBuilder =
                CronScheduleBuilder.cronSchedule("* * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(showTime())
                .withIdentity("dateTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
    //定义JobDetail
    @Bean
    public JobDetail stockAdd(){
        System.out.println("stockAdd方法运行了");
        return JobBuilder.newJob(StockJob.class)
                .withIdentity("stockAdd") //给JobDetail起名字
                .storeDurably() //即使没有触发器绑定当前JobDetail，也存在
                .build();
    }
    //声明触发器，它设置Job何时运行，也需要保存到Spring容器中
    @Bean
    public Trigger stockAddTrigger(){
        System.out.println("stockAddTrigger方法运行");
        //定义cron表达式
        CronScheduleBuilder cronScheduleBuilder =
                CronScheduleBuilder.cronSchedule("1,30 * 15 2 7 ?");
        return TriggerBuilder.newTrigger()
                .forJob(stockAdd())
                .withIdentity("stockAddTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
